// 在线房间创建和加入功能修复测试
// 测试各种边缘情况和错误处理

export class OnlineRoomFixTest {
  constructor() {
    this.testResults = []
  }

  // 测试房间号格式验证
  testRoomIdValidation() {
    console.log('=== 测试房间号格式验证 ===')
    
    const testCases = [
      { input: '', expected: false, description: '空房间号' },
      { input: '123', expected: false, description: '短房间号' },
      { input: 'room_1234567890_abc123', expected: true, description: '有效房间号' },
      { input: 'room_1234567890_abc123_extra', expected: true, description: '长房间号' }
    ]
    
    testCases.forEach(testCase => {
      const isValid = testCase.input.length >= 5
      const passed = isValid === testCase.expected
      
      console.log(`${testCase.description}: ${passed ? '✅ 通过' : '❌ 失败'}`)
      this.testResults.push({
        test: '房间号格式验证',
        case: testCase.description,
        passed: passed,
        input: testCase.input,
        expected: testCase.expected,
        actual: isValid
      })
    })
  }

  // 测试房间状态检查
  testRoomStatusValidation() {
    console.log('=== 测试房间状态验证 ===')
    
    const testCases = [
      {
        room: { status: 'waiting', player1: 'player1', player2: null },
        expected: true,
        description: '等待状态，可加入'
      },
      {
        room: { status: 'playing', player1: 'player1', player2: 'player2' },
        expected: false,
        description: '游戏进行中，不可加入'
      },
      {
        room: { status: 'waiting', player1: 'player1', player2: 'player2' },
        expected: false,
        description: '等待状态但有player2，不可加入'
      },
      {
        room: { status: 'finished', player1: 'player1', player2: 'player2' },
        expected: false,
        description: '游戏结束，不可加入'
      }
    ]
    
    testCases.forEach(testCase => {
      const canJoin = testCase.room.status === 'waiting' && !testCase.room.player2
      const passed = canJoin === testCase.expected
      
      console.log(`${testCase.description}: ${passed ? '✅ 通过' : '❌ 失败'}`)
      this.testResults.push({
        test: '房间状态验证',
        case: testCase.description,
        passed: passed,
        room: testCase.room,
        expected: testCase.expected,
        actual: canJoin
      })
    })
  }

  // 测试数据同步问题检测
  testDataSyncIssueDetection() {
    console.log('=== 测试数据同步问题检测 ===')
    
    const testCases = [
      {
        room: { status: 'waiting', player1: 'player1', player2: 'player2' },
        expected: 'sync_issue',
        description: 'waiting状态但有player2'
      },
      {
        room: { status: 'playing', player1: 'player1', player2: null },
        expected: 'data_inconsistency',
        description: 'playing状态但无player2'
      },
      {
        room: { status: 'playing', player1: 'player1', player2: 'player2' },
        expected: 'normal',
        description: '正常状态'
      }
    ]
    
    testCases.forEach(testCase => {
      let issueType = 'normal'
      
      if (testCase.room.status === 'waiting' && testCase.room.player2) {
        issueType = 'sync_issue'
      } else if (testCase.room.status === 'playing' && !testCase.room.player2) {
        issueType = 'data_inconsistency'
      }
      
      const passed = issueType === testCase.expected
      
      console.log(`${testCase.description}: ${passed ? '✅ 通过' : '❌ 失败'}`)
      this.testResults.push({
        test: '数据同步问题检测',
        case: testCase.description,
        passed: passed,
        room: testCase.room,
        expected: testCase.expected,
        actual: issueType
      })
    })
  }

  // 测试错误消息处理
  testErrorMessageHandling() {
    console.log('=== 测试错误消息处理 ===')
    
    const testCases = [
      {
        error: new Error('房间不存在'),
        expected: '房间不存在，请检查房间号',
        description: '房间不存在错误'
      },
      {
        error: new Error('房间已满，无法加入'),
        expected: '房间已满，无法加入',
        description: '房间已满错误'
      },
      {
        error: new Error('房间号格式不正确'),
        expected: '房间号格式不正确',
        description: '房间号格式错误'
      },
      {
        error: new Error('不能加入自己创建的房间'),
        expected: '不能加入自己创建的房间',
        description: '自己房间错误'
      },
      {
        error: new Error('网络连接失败'),
        expected: '网络连接失败，请重试',
        description: '网络错误'
      },
      {
        error: new Error('未知错误'),
        expected: '未知错误',
        description: '未知错误'
      }
    ]
    
    testCases.forEach(testCase => {
      let errorMessage = '加入房间失败'
      
      if (testCase.error.message.includes('房间不存在')) {
        errorMessage = '房间不存在，请检查房间号'
      } else if (testCase.error.message.includes('房间已满')) {
        errorMessage = '房间已满，无法加入'
      } else if (testCase.error.message.includes('房间号格式')) {
        errorMessage = '房间号格式不正确'
      } else if (testCase.error.message.includes('自己创建')) {
        errorMessage = '不能加入自己创建的房间'
      } else if (testCase.error.message.includes('网络')) {
        errorMessage = '网络连接失败，请重试'
      } else {
        errorMessage = testCase.error.message || '加入房间失败'
      }
      
      const passed = errorMessage === testCase.expected
      
      console.log(`${testCase.description}: ${passed ? '✅ 通过' : '❌ 失败'}`)
      this.testResults.push({
        test: '错误消息处理',
        case: testCase.description,
        passed: passed,
        error: testCase.error.message,
        expected: testCase.expected,
        actual: errorMessage
      })
    })
  }

  // 测试强制更新房间状态
  testForceUpdateRoomStatus() {
    console.log('=== 测试强制更新房间状态 ===')
    
    const testCases = [
      {
        currentRoom: { status: 'waiting', player1: 'player1', player2: 'player2' },
        updateData: { status: 'playing', currentPlayer: 'player2' },
        expected: true,
        description: '强制更新为playing状态'
      },
      {
        currentRoom: { status: 'playing', player1: 'player1', player2: null },
        updateData: { status: 'waiting', currentPlayer: null },
        expected: true,
        description: '强制更新为waiting状态'
      },
      {
        currentRoom: null,
        updateData: { status: 'playing', currentPlayer: 'player2' },
        expected: false,
        description: '房间不存在'
      }
    ]
    
    testCases.forEach(testCase => {
      let canUpdate = true
      
      if (!testCase.currentRoom) {
        canUpdate = false
      }
      
      const passed = canUpdate === testCase.expected
      
      console.log(`${testCase.description}: ${passed ? '✅ 通过' : '❌ 失败'}`)
      this.testResults.push({
        test: '强制更新房间状态',
        case: testCase.description,
        passed: passed,
        currentRoom: testCase.currentRoom,
        updateData: testCase.updateData,
        expected: testCase.expected,
        actual: canUpdate
      })
    })
  }

  // 运行所有测试
  runAllTests() {
    console.log('开始运行在线房间修复测试...')
    
    this.testRoomIdValidation()
    this.testRoomStatusValidation()
    this.testDataSyncIssueDetection()
    this.testErrorMessageHandling()
    this.testForceUpdateRoomStatus()
    
    this.printTestSummary()
  }

  // 打印测试总结
  printTestSummary() {
    console.log('\n=== 测试总结 ===')
    
    const totalTests = this.testResults.length
    const passedTests = this.testResults.filter(result => result.passed).length
    const failedTests = totalTests - passedTests
    
    console.log(`总测试数: ${totalTests}`)
    console.log(`通过: ${passedTests}`)
    console.log(`失败: ${failedTests}`)
    console.log(`成功率: ${((passedTests / totalTests) * 100).toFixed(2)}%`)
    
    if (failedTests > 0) {
      console.log('\n失败的测试:')
      this.testResults
        .filter(result => !result.passed)
        .forEach(result => {
          console.log(`- ${result.test}: ${result.case}`)
          console.log(`  期望: ${result.expected}, 实际: ${result.actual}`)
        })
    }
    
    return {
      total: totalTests,
      passed: passedTests,
      failed: failedTests,
      successRate: (passedTests / totalTests) * 100
    }
  }
}

// 导出测试类
export default OnlineRoomFixTest 